#!/bin/sh
# This script attempts to locate the jre directory of the current
# Java installation, even when java is not in the path
# It works with  the following rpm files:
#       Sun's Java Software Development Kit (Linux/Windows)
#       Sun's Java Runtime Environment (Linux)
#       IBM's Java Software Development Kit (linux)

# We require an option to specify which directory is desired:
# --java: directory with java executable
# --javac: directory with javac executable
# --jni: directory where JNI code is placed

if [ "$1" = "--jni" ]; then
    jni=yes
elif [ "$1" = "--java" ]; then
    java=yes
elif [ "$1" = "--javac" ]; then
    javac=yes
else
    echo "Usage: tos-locate-jre --java|--javac|--jni" >&2
    exit 2
fi

rpmlocate () {
    javarpm=$1
    javapath=`rpm -ql $1 2>/dev/null | grep "bin/$2$"`
}

pathlocate () {
    javapath=`which $1 2>/dev/null`
    while [ -n "$javapath" -a -h "$javapath" ]; do
	javapath=`readlink -q $javapath`
    done
    test -n "$javapath"
}

case `uname` in
    CYGWIN*)
    # Hopefully this will always work on cygwin with Sun's Java
    if [ "$javac" != "yes" ]; then #first try the public jre
	jversion=`regtool -q get '\\HKLM\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\CurrentVersion'`
	jhome=`regtool -q get '\\HKLM\SOFTWARE\\JavaSoft\\Java Runtime Environment\\'$jversion'\\JavaHome'`
	if [ "$jhome" == "" ] && [[ `uname` == *WOW64 ]]; then
	    jversion=`regtool -w -q get '\\HKLM\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\CurrentVersion'`
	    jhome=`regtool -w -q get '\\HKLM\SOFTWARE\\JavaSoft\\Java Runtime Environment\\'$jversion'\\JavaHome'`
	fi
    fi
    if [ "$jhome" == "" ]; then
	jversion=`regtool -q get '\\HKLM\\SOFTWARE\\JavaSoft\\Java Development Kit\\CurrentVersion'`
	jhome=`regtool -q get '\\HKLM\SOFTWARE\\JavaSoft\\Java Development Kit\\'$jversion'\\JavaHome'`
	if [ "$jhome" == "" ] && [[ `uname` == *WOW64 ]]; then
	    jversion=`regtool -w -q get '\\HKLM\\SOFTWARE\\JavaSoft\\Java Development Kit\\CurrentVersion'`
	    jhome=`regtool -w -q get '\\HKLM\SOFTWARE\\JavaSoft\\Java Development Kit\\'$jversion'\\JavaHome'`
	fi
    fi
    if [ "$jhome" == "" ]; then
	exit 1
    fi
    jhome=`cygpath -u "$jhome"`
    ;;

    Darwin)
    ## This should work bu the symlinks are broken. Apple bug, I suppose :)
    #jhome=/System/Library/Frameworks/JavaVM.framework/Versions/Current
    ## The real location is under /Developer/SDKs/MacOSX*.sdk/
    ## but now how do we determin the version correctly ?
    ## I have 10.6.6 but we need to drop the last digit
    ## `uname -r` returns 10.6.0 for some reason, and
    ## it appears that `basename `uname -r` .0` works
    ## it's not very certain if it will later.

    pn=`sw_vers -productName | sed 's/ //g'`
    pv=`sw_vers -productVersion | awk -F. '{ print $1 "." $2 }'`
    ## Since we only want to modify this one script for now, stick the
    ## extra subdir at the end, beceuase that's what the reset of scripts
    ## presume. This is a work-around and should be eliminated eventually.
    xcode_jdk=/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers
    jhome=/Library/Java/Home
    ;;

    Linux)
    # Check gentoo java configuration
    javapath=`java-config -c 2>/dev/null`
    # We check the path first, on the assumption that that's the preferred
    # version.
    if [ -z "$javapath" ]; then
	pathlocate javac || { test -z "$javac" && pathlocate java; }
    fi
    if [ -z "$javapath" ]; then
	# We try a bunch of standard names, before resorting to rpm -qa
	rpmlocate IBMJava2-SDK javac || \
	    rpmlocate IBMJava2-142-ia32-SDK javac || \
	    rpmlocate j2sdk javac || \
	    rpmlocate jdk javac || \
	    { test -z "$javac" && rpmlocate j2re java; } || \
	    { test -z "$javac" && rpmlocate jre java; }

	if [ -z "$javapath" ]; then
	    # lastly, check for a weirdly named IBMJava2
	    name=`rpm -qa | grep IBMJava2 | head -1`
	    if [ -n "$name" ]; then
		rpmlocate $name javac
	    fi
	fi
    fi
    if [ -z "$javapath" ]; then
	exit 1
    fi
    jbin=`dirname "$javapath"`
    jhome=`dirname "$jbin"`
    ;;

    FreeBSD)
    # We check the path first, on the assumption that that's the preferred
    # version.
    pathlocate javac || { test -z "$javac" && pathlocate java; }
    if [ -n "$javapath" ]; then
	jbin=`dirname "$javapath"`
    else
	if [ -f /usr/local/jdk1.4*/bin/java ]; then
	    jbin=/usr/local/jdk1.4*/bin
	else
	    exit 1
	fi
    fi
    jhome=`dirname $jbin`
    ;;
esac

# These are correct for Sun and IBM's x86 Java versions
if [ "$jni" = "yes" ]; then
    jnilocate () {
	dir="$1"
	test -d "$1"
    }

    # Look for a likely JNI directory
    # Windows, and IBM Java: in jre/bin
    # Sun Java on Linux: in jre/lib/i386
    if [ `uname` = "Darwin" ]; then
	jnilocate "/Library/Java/Extensions"
    elif "$jhome/bin/java" -version 2>&1 | grep -q IBM || cygpath -w / >/dev/null 2>/dev/null; then
	jnilocate "$jhome/jre/bin" || jnilocate "$jhome/bin"
    else
	arch=`uname -m`
	jnilocate "$jhome/jre/lib/$arch" || \
	    jnilocate "$jhome/jre/lib/i386" || \
	    jnilocate "$jhome/jre/lib/amd64" || \
	    jnilocate "$jhome/lib/$arch" || \
	    jnilocate "$jhome/lib/i386" || \
	    jnilocate "$jhome/lib/amd64" || \
	    jnilocate "$jhome/lib/arm" || \
	    jnilocate "$jhome/lib"
    fi
elif [ "$javac" = "yes" ]; then
 if [ `uname` = "Darwin" ];  then
  dir="$xcode_jdk"
 else
  dir="$jhome/bin"
 fi
elif [ "$java" = "yes" ]; then
 dir="$jhome/bin"
fi

# Check that what we found actually exists
if [ -d "$dir" ]; then
 echo $dir
else
 exit 1
fi
